package com.winit.page.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Update;
import com.winit.page.model.PageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

@Repository
public class PageElementRepositoryExtImpl implements IPageElementRepositoryExt {

    @Autowired
    private CassandraOperations cassandraOperations;

    @Override
    public int updatePropertiesNotNullById(PageElement pageElement) {
        Update update = QueryBuilder.update("matrix_page_element");
        if (!StringUtils.isEmpty(pageElement.getComponentId())){
            update.with(QueryBuilder.set("ref_component_id", pageElement.getComponentId()));
        }

        if (!StringUtils.isEmpty(pageElement.getMasterConfigId())){
            update.with(QueryBuilder.set("ref_master_config_id", pageElement.getMasterConfigId()));
        }

        if (!StringUtils.isEmpty(pageElement.getPageId())){
            update.with(QueryBuilder.set("ref_page_id", pageElement.getPageId()));
        }

        if (!StringUtils.isEmpty(pageElement.getParentId())){
            update.with(QueryBuilder.set("parent_id", pageElement.getParentId()));
        }
        if (null != pageElement.getChildren()){
            update.with(QueryBuilder.set("children", pageElement.getChildren()));
        }
        update.where(QueryBuilder.eq("id", pageElement.getId()));
        cassandraOperations.execute(update);
        return 1;
    }
}
